package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.chapter.VideoVo;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author 陈烨庆
 * @since 2022-01-24
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService eduVideoService;

    /**
     * 课程大纲列表，根据课程id进行查询
     *
     * @param courseId
     * @return
     */
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {


        // 1、查询章节数据
        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id", courseId);
        wrapperChapter.orderByAsc("sort");
        List<EduChapter> chapterList = baseMapper.selectList(wrapperChapter);

        // 2、查询小节
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id", courseId);
        wrapperChapter.orderByAsc("sort");
        List<EduVideo> videoList = eduVideoService.list(wrapperVideo);

        // 最终的返回数据
        List<ChapterVo> finalList = new ArrayList<>();

        // 3、遍历查询章节list集合进行封装
        for (EduChapter eduChapter : chapterList) {
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter, chapterVo);
            finalList.add(chapterVo);

            // 4、遍历查询小节list集合进行封装
            // 封装小节列表
            ArrayList<VideoVo> childrenList = new ArrayList<>();
            for (EduVideo eduVideo : videoList) {
                if (eduChapter.getId().equals(eduVideo.getChapterId())) {
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo, videoVo);
                    childrenList.add(videoVo);
                }
            }
            chapterVo.setChildren(childrenList);
        }

        return finalList;
    }

    @Override
    public Boolean removeChapter(String id) {
        QueryWrapper<EduVideo> videoWrapper = new QueryWrapper<>();
        videoWrapper.eq("chapter_id", id);
        int videoCount = eduVideoService.count(videoWrapper);
        if (videoCount > 0) {
            throw new GuliException(20001, "还有未删除的小节数据");
        } else {
            return baseMapper.deleteById(id) > 0;
        }
    }

    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> videoWrapper = new QueryWrapper<>();
        videoWrapper.eq("course_id", courseId);
        baseMapper.delete(videoWrapper);
    }
}
